package org.fhm.zdte.common.pojo;

import org.fhm.zdte.common.constant.protocol.ConnectStatus;

import java.io.Serializable;
import java.net.InetSocketAddress;

/**
 * Customize a socket address object.
 *
 * @author 谭波
 * @since 2023/12/22
 */
public class Address implements Serializable {

    private static final long serialVersionUID = 1215720748342549860L;
    private final String ipv4;
    private int port;
    private ConnectStatus status;

    public Address(InetSocketAddress address) {
        this.port = address.getPort();
        this.ipv4 = address.getHostString();
    }

    public Address(String ipv4, int port) {
        this.port = port;
        this.ipv4 = ipv4;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getIpv4() {
        return ipv4;
    }

    public ConnectStatus getStatus() {
        return status;
    }

    public void setStatus(ConnectStatus status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Address)) return false;

        Address address = (Address) o;

        if (getPort() != address.getPort()) return false;
        return getIpv4().equals(address.getIpv4());
    }

    @Override
    public int hashCode() {
        int result = getPort();
        result = 31 * result + getIpv4().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "ipV4: " + ipv4 + ", port: " + port;
    }
}
